﻿using BySoft.Server.Business;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.MasteData
{
    public partial class SupplierList : AuthBasePage
    {
        int m_count = 0;
        public int IPageIndex
        {
            get
            {
                if (null == Session["SupplierIPageIndex"])
                {
                    return 0;
                }
                else
                {
                    return (int)Session["SupplierIPageIndex"];
                }
            }
            set
            {
                Session["SupplierIPageIndex"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDL();
                BindData(0);
            }
        }
        private void BindDDL()
        {
            BindParameterForListControl("SupplierScaler", ddlSupplierScaler);
            UIHelper.InsertFirstItem(ddlSupplierScaler);
        }
        private void BindData(int pageIndex)
        {
            //SetQuery();
            pageIndex = IPageIndex;
            DataTable dt = MdSupplier.SelectList(txtSupplierID.Text.Trim(), txtSupplierName.Text.Trim(), this.ddlSupplierScaler.SelectedValue, pageIndex, pcData.PageSize, out m_count);
            pcData.RecordCount = m_count;
            if (dt.Rows.Count != 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();
                pcData.CurrentIndex = pageIndex;
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

        protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strCode = e.CommandArgument.ToString();
            string[] strs = strCode.Split(',');
            switch (e.CommandName)
            {
                case "viewdata":
                    Response.Redirect(string.Format("SupplierView.aspx?id={0}", HttpUtility.UrlEncode(strCode)));
                    break;
                case "edi":
                    Response.Redirect(string.Format("SupplierEdit.aspx?id={0}", HttpUtility.UrlEncode(strCode)));
                    break;
                case "del":
                    MdSupplierdetail.Del(strCode);
                    MdSupplier.Del(strCode);
                    BindData(0);
                    UIHelper.AlertMessage(this.Page, Message.DeleteSuccess);
                    break;
                default:
                    break;
            }
        }

        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton ibtn = (ImageButton)e.Row.FindControl("ibtnDel");
                Button btnDel = (Button)e.Row.FindControl("btnDel");
                if (ibtn != null)
                {
                    ibtn.OnClientClick = "ShowTopConfirm('提示信息','" + Message.ConfirmDelete + "','confirmCallback(\\'" + btnDel.ClientID + "\\')');return false;";
                }
            }
        }

        protected void pcData_PageIndexClick(object sender, Controls.PageIndexClickEventArgs e)
        {
            IPageIndex = e.PageIndex;
            BindData(e.PageIndex);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData(0);
        }
    }
}